package com.whitecat.voting.service;

import com.google.common.collect.Lists;
import com.whitecat.voting.constant.Province;
import com.whitecat.voting.constant.SysConstant;
import com.whitecat.voting.dto.MaterialDTO;
import com.whitecat.voting.entity.Candidate;
import com.whitecat.voting.entity.MpUserInfo;
import com.whitecat.voting.util.MaterialUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

@Slf4j
@Service
public class MaterialService {

    private final CandidateService candidateService;
    private final MpUserInfoService mpUserInfoService;
    private final VotingRecordService votingRecordService;

    public MaterialService(CandidateService candidateService, MpUserInfoService mpUserInfoService, VotingRecordService votingRecordService) {
        this.candidateService = candidateService;
        this.mpUserInfoService = mpUserInfoService;
        this.votingRecordService = votingRecordService;
    }

    public MaterialDTO findByOpenId(String openId) {
        MpUserInfo info = mpUserInfoService.findById(openId);
        if (info != null) {
            Candidate candidate = candidateService.findById(openId);
            if (candidate == null) {
                return null;
            }
            return MaterialDTO.builder()
                    .openId(openId)
                    .name(candidate.getName())
                    .region(Province.fromCode(candidate.getRegion()).getFullName())
                    .introduction(candidate.getIntroduction())
                    .nickName(info.getNickName())
                    .avatarUrl(info.getAvatarUrl())
                    .cover(getCover(openId))
                    .video(getVideo(openId))
                    .votesCount(votingRecordService.countVotesByOpenId(openId))
                    .build();
        }

        return null;
    }

    @Async
    public void generateScreenshot(File video) {
        File parentDir = video.getParentFile();
        File screenshot = FileUtils.getFile(parentDir, "screenshot.jpeg");
        if (!screenshot.exists()) {
            log.info("generating screenshot for [{}]", screenshot.getAbsolutePath());
            MaterialUtils.extractScreenshot(video);
        }
    }

    public List<MaterialDTO> listAll() {
        List<MaterialDTO> materials = Lists.newArrayList();
        List<Candidate> allCandidates = candidateService.findPage(1, 0);
        for (Candidate candidate : allCandidates) {
            String openId = candidate.getOpenId();
            MaterialDTO material = this.findByOpenId(openId);
            if (material == null) {
                log.warn("Invalid openid: [{}]", openId);
            }

            materials.add(material);
        }

        return materials;
    }

    private String getCover(String openId) {
        File video = FileUtils.getFile(System.getProperty("user.dir"), SysConstant.DEFAULT_MATERIAL_DIR, openId, openId + ".mp4");
        return video.exists() ? SysConstant.DEFAULT_MATERIAL_DIR + "/" + openId + "/thumb.jpeg" : null;
    }

    public String getVideo(String openId) {
        File video = FileUtils.getFile(System.getProperty("user.dir"), SysConstant.DEFAULT_MATERIAL_DIR, openId, openId + ".mp4");
        return video.exists() ? SysConstant.DEFAULT_MATERIAL_DIR + "/" + openId + "/" + openId + ".mp4" : null;
    }
}
